import express from 'express'
import fs from 'fs'
import https from 'https'
import path from 'path'
import { Server } from 'socket.io'
import { getIpAddress } from './utils/index.js'
import { initSdpServer } from './server/single-sdp.js'

const app = express()

app.use(express.static('public'))

const options = {
  key: fs.readFileSync('./ssl/privkey.pem'),
  cert: fs.readFileSync('./ssl/fullchain.pem')
}

const httpsServer = https.createServer(options, app)
const io = new Server(httpsServer, {
  allowEIO3: true,
  cors: true
})
initSdpServer(io)

httpsServer.listen(3000, () => {
  let ipAddress = getIpAddress()
    ? 'https://' + getIpAddress() + ':3000'
    : 'https://localhost:3000'

  console.log('启动成功: ' + ipAddress)
})
